package com.lfz.sso.controller;

import com.lfz.common.vo.SysResult;
import com.lfz.sso.pojo.User;
import com.lfz.sso.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping(value = "/user/{username}")
    public SysResult getUser(String username){
        if (StringUtils.isEmpty(username)){
            return SysResult.build(400, "参数错误");
        }

        try{
            User user = userService.queryUserOne(username);
            if (null != user) {
                return SysResult.oK(user);
            }
            return SysResult.build(401, "用户名不存在！");
        } catch (Exception e) {
            e.printStackTrace();
            return SysResult.build(201, "登陆失败，请稍候重试");
        }

    }

    @GetMapping(value = "/user/")
    public SysResult listUser(){
        try {
            List<User> list = userService.queryUserAll();
            return SysResult.oK(list);
        } catch (Exception e) {
            e.printStackTrace();
            return SysResult.build(500, "查询失败，请稍候重试");
        }
    }

}
